package com.kkd.kkdapp.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.kkd.kkdapp.R;

public class HistogramView extends View {

	private Paint xLinePaint;// 坐标轴 轴线 画笔：
	private Paint hLinePaint;// 坐标轴水平内部 虚线画笔
	private Paint titlePaint;// 绘制文本的画笔
	private Paint paint;// 矩形画笔 柱状图的样式信息
	private int[] progress;// 7 条
	private int[] aniProgress;// 实现动画的值
	private final int TRUE = 1;// 在柱状图上显示数字
	private int[] text;
	// 微调偏移量
	private int mLen = dip2px(getContext(), 21);
	// 坐标轴左侧的数标
	// 整体下移量
	private int mYlen = dip2px(getContext(), 30);
	private String[] ySteps;
	// 坐标轴底部的星期数

	private String[] xWeeks;

	// private HistogramAnimation ani;

	public HistogramView(Context context) {
		super(context);
		init(context, null);
	}

	public HistogramView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {

		ySteps = new String[] { "10k", "7.5k", "5k", "2.5k", "0" };
		xWeeks = new String[] { "银行", "宝宝类", "快快贷活期理财" };
		text = new int[] { 0, 0, 0 };
		aniProgress = new int[] { 0, 0, 0 };
		// ani = new HistogramAnimation();
		// ani.setDuration(1000);

		xLinePaint = new Paint();
		hLinePaint = new Paint();
		titlePaint = new Paint();
		paint = new Paint();

		xLinePaint.setColor(Color.DKGRAY);
		hLinePaint.setColor(Color.LTGRAY);
		titlePaint.setColor(Color.BLACK);
	}

	public void setText(int[] text) {

		this.text = text;

		this.postInvalidate();// 可以子线程 更新视图的方法调用。
	}

	public void setProgress(int[] progress) {
		this.progress = progress;
		// this.invalidate(); //失效的意思。
		// this.postInvalidate(); // 可以子线程 更新视图的方法调用。
		// this.startAnimation(ani);
		for (int i = 0; i < aniProgress.length; i++) {
			aniProgress[i] = progress[i];
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return super.onTouchEvent(event);
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		int width = getWidth();
		int height = getHeight() - dip2px(getContext(), 66);

		// 1 绘制坐标线：startX, startY, stopX, stopY, paint
		int startX = dip2px(getContext(), 50);
		int startY = dip2px(getContext(), 10);
		int stopX = dip2px(getContext(), 50);
		int stopY = dip2px(getContext(), 320);
		// 画Y轴
		// canvas.drawLine(dip2px(getContext(), 17), dip2px(getContext(), 3),
		// dip2px(getContext(), 17), height, xLinePaint);

		drawAL(dip2px(getContext(), 17), height + mYlen, dip2px(getContext(), 17),
				dip2px(getContext(), 3) + mYlen, canvas);
		// 画X轴
		// canvas.drawLine(dip2px(getContext(), 17), height,
		// width - dip2px(getContext(), 3), height, xLinePaint);

		drawAL(dip2px(getContext(), 17), height + mYlen,
				width - dip2px(getContext(), 6), height + mYlen, canvas);
		// 2 绘制坐标内部的水平线

		int leftHeight = height - dip2px(getContext(), 6);// 左侧外周的 需要划分的高度：

		int hPerHeight = leftHeight / 4;// 分成四部分

		// hLinePaint.setTextAlign(Align.CENTER);
		// for (int i = 0; i < 4; i++) {
		// canvas.drawLine(dip2px(getContext(), 17), dip2px(getContext(), 6)
		// + i * hPerHeight, width - dip2px(getContext(), 3),
		// dip2px(getContext(), 6) + i * hPerHeight, hLinePaint);
		// }

		// 3 绘制 Y 周坐标

		titlePaint.setTextAlign(Align.RIGHT);
		titlePaint.setTextSize(35);
		titlePaint.setAntiAlias(true);
		titlePaint.setStyle(Paint.Style.FILL);
		// for (int i = 0; i < ySteps.length; i++) {
		// canvas.drawText(ySteps[i], dip2px(getContext(), 15),
		// dip2px(getContext(), 8) + i * hPerHeight, titlePaint);
		// }

		// 4 绘制 X 周 做坐标
		int xAxisLength = width - dip2px(getContext(), 9);
		int columCount = xWeeks.length + 1;
		int step = xAxisLength / columCount;
		// for (int i = 0; i < columCount - 1; i++) {
		// // text, baseX, baseY, textPaint
		// canvas.drawText(xWeeks[i], 55 + step * (i + 1) + mLen * (i - 1),
		// height + 30, titlePaint);
		// }

		canvas.drawText(xWeeks[0], dip2px(getContext(), 22) + step * (0 + 1)
				+ mLen * (0 - 1), height + dip2px(getContext(), 11) + mYlen, titlePaint);
		canvas.drawText(xWeeks[1], dip2px(getContext(), 26) + step * (1 + 1)
				+ mLen * (1 - 1) + dip2px(getContext(), 3),
				height + dip2px(getContext(), 11) + mYlen, titlePaint);
		canvas.drawText(xWeeks[2], dip2px(getContext(), 36) + step * (2 + 1)
				+ mLen * (2 - 1) + dip2px(getContext(), 16),
				height + dip2px(getContext(), 11) + mYlen, titlePaint);
		// 5 绘制矩形

		if (aniProgress != null && aniProgress.length > 0) {
			for (int i = 0; i < aniProgress.length; i++) {// 循环遍历将7条柱状图形画出来
				int value = aniProgress[i];
				paint.setAntiAlias(true);// 抗锯齿效果
				paint.setStyle(Paint.Style.FILL);
				paint.setTextSize(20);// 字体大小
				paint.setColor(Color.parseColor("#6DCAEC"));// 字体颜色
				Rect rect = new Rect();// 柱状图的形状

				rect.left = dip2px(getContext(), 10) + step * (i + 1)
						- dip2px(getContext(), 30) + mLen * (i - 1);
				rect.right = dip2px(getContext(), 10) + step * (i + 1)
						+ dip2px(getContext(), 30) + mLen * (i - 1);
				int rh = (int) (leftHeight - leftHeight * (value / 10000.0));
				rect.top = rh + dip2px(getContext(), 6) + mYlen;
				rect.bottom = height + mYlen;

				Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
						R.drawable.column);

				canvas.drawBitmap(bitmap, null, rect, paint);

				if (true) {
					canvas.drawText(value + "", dip2px(getContext(), 12) + step
							* (i + 1) - dip2px(getContext(), 9) + mLen
							* (i - 1), rh + dip2px(getContext(), 3) + mYlen, paint);
				}

			}
		}

	}

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	private class HistogramAnimation extends Animation {
		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			super.applyTransformation(interpolatedTime, t);
			if (interpolatedTime < 1.0f) {
				for (int i = 0; i < aniProgress.length; i++) {
					aniProgress[i] = (int) (progress[i] * interpolatedTime);
				}
			} else {
				for (int i = 0; i < aniProgress.length; i++) {
					aniProgress[i] = progress[i];
				}
			}
			postInvalidate();
		}
	}

	public void drawAL(int sx, int sy, int ex, int ey, Canvas myCanvas) {
		double H = 12; // 箭头高度
		double L = 5; // 底边的一半
		int x3 = 0;
		int y3 = 0;
		int x4 = 0;
		int y4 = 0;
		double awrad = Math.atan(L / H); // 箭头角度
		double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度
		double[] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true, arraow_len);
		double[] arrXY_2 = rotateVec(ex - sx, ey - sy, -awrad, true, arraow_len);
		double x_3 = ex - arrXY_1[0]; // (x3,y3)是第一端点
		double y_3 = ey - arrXY_1[1];
		double x_4 = ex - arrXY_2[0]; // (x4,y4)是第二端点
		double y_4 = ey - arrXY_2[1];
		Double X3 = new Double(x_3);
		x3 = X3.intValue();
		Double Y3 = new Double(y_3);
		y3 = Y3.intValue();
		Double X4 = new Double(x_4);
		x4 = X4.intValue();
		Double Y4 = new Double(y_4);
		y4 = Y4.intValue();
		// 画线
		myCanvas.drawLine(sx, sy, ex, ey, xLinePaint);
		Path triangle = new Path();
		triangle.moveTo(ex, ey);
		triangle.lineTo(x3, y3);
		triangle.lineTo(x4, y4);
		triangle.close();
		myCanvas.drawPath(triangle, xLinePaint);

	}

	// 计算
	public double[] rotateVec(int px, int py, double ang, boolean isChLen,
			double newLen) {
		double mathstr[] = new double[2];
		// 矢量旋转函数，参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度
		double vx = px * Math.cos(ang) - py * Math.sin(ang);
		double vy = px * Math.sin(ang) + py * Math.cos(ang);
		if (isChLen) {
			double d = Math.sqrt(vx * vx + vy * vy);
			vx = vx / d * newLen;
			vy = vy / d * newLen;
			mathstr[0] = vx;
			mathstr[1] = vy;
		}
		return mathstr;
	}
}